<template>
  <div :class="$style.wrap">
    <ContentWrap>
      <BaseInfo :info="info" @maxAreaShop="maxAreaShopChange"></BaseInfo>
      <Title title="客情信息" :title-class="$style.title" style="padding-top:10px"></Title>
      <CareInput :detail="detail" :default-address="defaultAddress"></CareInput>
    </ContentWrap>
  </div>
</template>
<script>
import ContentWrap from '@/components/ContentWrap'
import BaseInfo from './components/baseInfo'
import CareInput from './components/input'
import Title from '@/components/Title'
import { getCustomerRefPage } from '@/api/mscInfo'
import { careCustomerDetail } from '@/api/guestCare'

export default {
  name: 'ClientManageEdit',
  components: {
    ContentWrap,
    BaseInfo,
    CareInput,
    Title
  },
  data() {
    return {
      info: {},
      credentialNo: '',
      id: '',
      detail: {},
      defaultAddress: ''
    }
  },
  created() {
    this.credentialNo = this.$route.query.credentialNo
    this.id = this.$route.query.id
    if (this.credentialNo) {
      this.getDealerInfo()
    }
    if (this.id) {
      this.getDetail()
    }
  },
  methods: {
    // 获取经销商信息
    getDealerInfo() {
      getCustomerRefPage({ pageFlag: false, credentialNo: this.credentialNo }).then(res => {
        const data = res.data.records || []
        let legalPerson = ''
        const ids = []
        const businessStatus = []
        const customizeAccount = []
        // 【所属办事处名称】+【账号类型】+客户经理+客户经理电话
        data.forEach(item => {
          legalPerson = item.legalPerson
          ids.push(item.id)
          businessStatus.push(Number(item.businessStatus))
          // 电话号码及姓名同时存在则不添加
          for (const list of customizeAccount) {
            if (list.tel === item.accountManagerPhone && list.name === item.accountManager) {
              return
            }
          }
          if (item.accountManager) {
            customizeAccount.push({ officeName: item.organizationName, accountType: '成品客户经理', name: item.accountManager, tel: item.accountManagerPhone })
          }
          if (item.customizeAccountManager) {
            customizeAccount.push({ officeName: item.organizationName, accountType: '定制客户经理', name: item.customizeAccountManager, tel: item.customizeAccountManagerPhone })
          }
        })
        const info = {}
        info.businessStatus = Math.min(...businessStatus)
        info.legalPerson = legalPerson
        info.credentialNo = this.credentialNo
        info.customizeAccount = customizeAccount
        info.ids = ids
        this.info = info
      })
    },
    // 获取关怀客户详情
    getDetail() {
      careCustomerDetail(this.id).then(res => {
        this.detail = res.data
      })
    },
    // 最大门店地址
    maxAreaShopChange(v) {
      this.defaultAddress = v.shopAddr
    }
  }
}
</script>
<style lang='scss' module>
.wrap{
.title{
  color: rgba(0,0,0,0.85);
  font-size: 16px;
  font-weight: 700;
}
}
</style>
